"""
学生接口
"""
from db import modols


def register_interface(username, password):
    # 用户不存在
    if modols.Student.select(username):
        return False, '用户已存在！'
    # 用户存在
    student_obj = modols.Student(username, password)
    student_obj.save()
    return True, '注册成功'


# def login_interface(username, password):
#     # 用户不存在
#     student_obj = models.Student.select(username)
#     if not student_obj:
#         return False, '用户不存在！'
#     if student_obj.password == password:
#         return True, '登录成功！'
#     else:
#         return False, '密码错误'


def select_school(school_name, student_name):
    # 判读学生是否已经选择了学校
    student_obj = modols.Student.select(student_name)
    if student_obj.school:
        return False, '该学生已经选择过校区了'

    student_obj.select_school(school_name)
    return True, '学校选择成功'


def get_course_list_interface(student_name):
    student_obj = modols.Student.select(student_name)
    if not student_obj.school:
        return False, '没有学校，请先选择学校'
    school_obj = modols.School.select(student_obj.school)
    if not school_obj.course_list:
        return False, '该学校没有课程，请先联系管理员'
    return True, school_obj.course_list


def select_course_interface(course_name, student_name):
    student_obj = modols.Student.select(student_name)
    if course_name in student_obj.course_list:
        return False, '该课程已经选择过了'
    student_obj.select_course(course_name)
    return True, f'{course_name}课程选择成功'


def check_score_interface(student_name):
    student_obj=modols.Student.select(student_name)
    return student_obj.score
